package org.oslims.client.uicomponents.editbox;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.DateInputField;
import org.oslims.client.uicomponents.inputfields.DropDownInputField;
import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.RichTextInputField;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.Sample;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditSampleBox {

	final DialogBox editSampleBox = new DialogBox(false, true);
	final VerticalPanel editSampleBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField originField = new TextInputField("origin", "Origin: ", "120px", "200px");
	final RichTextInputField descriptionField = new RichTextInputField("description", "Description: ", "120px", "400px");
	final DateInputField samplingDateField = new DateInputField("samplingTime", "Sampling Date: ", "120px", "200px");
	final DropDownInputField sampleTypeField = new DropDownInputField("sampleType", "Sample Type: ", "120px", "200px");

	Sample editedSample;

	String editedType;

	public EditSampleBox() {

		editSampleBoxPanel.add(originField);
		inputFields.put(originField.getFieldName(), originField);

		editSampleBoxPanel.add(descriptionField);
		inputFields.put(descriptionField.getFieldName(), descriptionField);

		editSampleBoxPanel.add(samplingDateField);
		inputFields.put(samplingDateField.getFieldName(), samplingDateField);

		editSampleBoxPanel.add(sampleTypeField);
		inputFields.put(sampleTypeField.getFieldName(), sampleTypeField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editSampleBox.add(editSampleBoxPanel);

		// Add the save and cancel buttons
		editSampleBoxPanel.add(buttonLine);
	}

	public void hide() {
		editSampleBox.hide();
	}

	public void clear() {
		editedSample = null;
		originField.getInputField().setValue("");
		descriptionField.getInputHtml().setHTML("");
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();
		sampleTypeField.getInputBox().setSelectedIndex(-1);
		editSampleBox.setText("Create new Sample");
		editSampleBox.center();
	}

	public void showEdit(Sample sample) {
		this.clear();
		this.clearAllErrors();

		editedSample = sample;
		originField.getInputField().setValue(sample.getOrigin());
		descriptionField.getInputHtml().setHTML(sample.getDescription());
		if (sample.getSamplingTime() != null)
			samplingDateField.getInputField().setValue(sample.getSamplingTime());

		editSampleBox.setText("Edit Sample");
		editSampleBox.center();
	}

	public void showError(String fieldName, String message) {
		GWT.log(fieldName);
		inputFields.get(fieldName).showError(message);
	}

	public void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	public void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {
			inputField.clearError();
		}
	}

	public HasValue<String> getOrigin() {
		return originField.getInputField();
	}

	public HasHTML getDescription() {
		return descriptionField.getInputHtml();
	}

	public HasValue<Date> getSamplingTime() {
		return samplingDateField.getInputField();
	}

	public DropDownInputField getSampleType() {
		return sampleTypeField;
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	public Sample getSample() {
		return editedSample;
	}
}
